package com.dxy.app.widget.shrink_upvote;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import java.util.ArrayList;
import java.util.List;

/**
  *Created by duxingyu on 2017/7/19.17:59
  *Email: 
  *Description:差评表情
  **/
public class UpvoteDislikeView extends ShrinkUpvoteView {

  /**
   * 轨迹列表
   * */
  private List<Integer> list;
  /**
   * 特色画笔
   * */
  private Paint mouthPaint;
  /**
   * 当前偏移量
   * */
  private int curDegree = -20;
  /**
   * 每次状态的更新量
   * */
  public static final int INCRES_DEGREE=3;
  /**
   * 最大偏移量
   * */
  public static final int MAX_OFFSET_DEGREE=20;
  /**
   * 绘制挤眼睛
   * */
  private float shrinkDelX =0;

  public UpvoteDislikeView(Context context) {
    super(context);
  }

  public UpvoteDislikeView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
  }

  public UpvoteDislikeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
  }

  @Override protected void initSelfParams() {
    mouthPaint = new Paint();
    mouthPaint.setAntiAlias(true);
    mouthPaint.setStrokeWidth(eyeRadius / 2);
    mouthPaint.setColor(colorBorder);
    mouthPaint.setStrokeCap(Paint.Cap.ROUND);

    list = new ArrayList<>();
    list.add(MAX_OFFSET_DEGREE);
    list.add(0-MAX_OFFSET_DEGREE);
    list.add(MAX_OFFSET_DEGREE);
    list.add(0-MAX_OFFSET_DEGREE);

    type =VoteType.DISLIKE;
  }


  @Override protected void drawFace(Canvas canvas, float offsetY) {
    float eyeDelY =positionY-radius -eyeToTopY- offsetY;

    paint.setColor(colorBorder);
    canvas.drawCircle(positionX - eyeToMiddleX+shrinkDelX, eyeDelY-shrinkDelX, eyeRadius, paint);
    canvas.drawCircle(positionX + eyeToMiddleX-shrinkDelX, eyeDelY-shrinkDelX, eyeRadius, paint);
    canvas.save();

    mouthPaint.setColor(colorBorder);
    float mouthDelY =positionY -radius +eyeToTopY*3/2-offsetY;
    canvas.rotate(curDegree, positionX, mouthDelY);
    canvas.drawLine(positionX - eyeToMiddleX, mouthDelY, positionX + eyeToMiddleX,mouthDelY, mouthPaint);
    canvas.rotate(curDegree, positionX, mouthDelY);

    canvas.restore();
  }

  @Override protected void shrinkControl() {
    for (int each : list) {
      if (curDegree <= each) {
        for (int k = curDegree; k < each; k++) {
          curDegree += INCRES_DEGREE;
          shrinkDelX +=1/3f;
          if (curDegree >= each) {
            curDegree = each;
            break;
          }
          postInvalidate();
          try {
            Thread.sleep(SHRINK_TIME);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
      } else {
        for (int k = curDegree; k > each; k--) {
          curDegree -= INCRES_DEGREE;
          shrinkDelX -=1/3f;
          if (curDegree <= each) {
            curDegree = each;
            shrinkDelX=0;//归零处理
            break;
          }
          postInvalidate();
          try {
            Thread.sleep(SHRINK_TIME);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
      }
    }
  }
}
